﻿using Common.Device.Enums;
using Common.Device.Models;
using Common.Tool.DataTools;
using HandyControl.Expression.Shapes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// JueYuanNaiYaView.xaml 的交互逻辑
    /// </summary>
    public partial class JueYuanNaiYaView : UserControl
    {
        ViewModels.JueYuanNaiYaViewModel viewModel =new ViewModels.JueYuanNaiYaViewModel();
        public JueYuanNaiYaView(DeviceEnum.DeviceModelEnum model, Order order = null)
        {
            InitializeComponent();
            //根据仪器型号加载界面
            switch (model)
            {
                case DeviceEnum.DeviceModelEnum.TH2689: viewModel.HasLc = true; viewModel.MenuIndex = 3; viewModel.ArcUnit = ""; break;
                case DeviceEnum.DeviceModelEnum.TH9201B: viewModel.HasIr = true; viewModel.HasDcw = true; viewModel.HasAcw = true; viewModel.ArcUnit = "mA"; break;
                case DeviceEnum.DeviceModelEnum.AT686: viewModel.HasIr = true; viewModel.HasDcw = true; viewModel.HasAcw = true; viewModel.ArcUnit = ""; break;
                case DeviceEnum.DeviceModelEnum.EEC7473: viewModel.HasAcw = true; viewModel.MenuIndex = 2; viewModel.ArcUnit = ""; break;
                default:
                    break;
            }
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out AnGuiYiEnum.TestMode mode);
                JueYuanNaiYa parameter = JsonConvert.DeserializeObject<JueYuanNaiYa>(order.deviceModel);
                switch (mode)
                {
                    case AnGuiYiEnum.TestMode.绝缘电阻:
                        viewModel.MenuIndex = 0;
                        viewModel.IrVol = parameter.电压值.ToString();
                        viewModel.IrUpTime = parameter.上升时间.ToString();
                        viewModel.IrKeepTime = parameter.测试时间.ToString();
                        viewModel.IrDownTime = parameter.下降时间.ToString();
                        viewModel.IrMin = parameter.下限值?.ToString();
                        viewModel.IrMax = parameter.上限值?.ToString();
                        break;
                    case AnGuiYiEnum.TestMode.直流耐压:
                        viewModel.MenuIndex = 1;
                        viewModel.DcwVol = parameter.电压值.ToString();
                        viewModel.DcwUpTime = parameter.上升时间.ToString();
                        viewModel.DcwKeepTime = parameter.测试时间.ToString();
                        viewModel.DcwDownTime = parameter.下降时间.ToString();
                        viewModel.DcwMin = parameter.下限值?.ToString();
                        viewModel.DcwMax = parameter.上限值?.ToString();
                        viewModel.DcwArc = parameter.电弧值.ToString();
                        break;
                    case AnGuiYiEnum.TestMode.交流耐压:
                        viewModel.MenuIndex = 2;
                        viewModel.AcwVol = parameter.电压值.ToString();
                        viewModel.AcwUpTime = parameter.上升时间.ToString();
                        viewModel.AcwKeepTime = parameter.测试时间.ToString();
                        viewModel.AcwDownTime = parameter.下降时间.ToString();
                        viewModel.AcwMin = parameter.下限值?.ToString();
                        viewModel.AcwMax = parameter.上限值?.ToString();
                        viewModel.AcwArc = parameter.电弧值.ToString();
                        break;
                    case AnGuiYiEnum.TestMode.漏电流:
                        viewModel.MenuIndex = 3;
                        viewModel.LcVol = parameter.电压值.ToString();
                        viewModel.LcUpTime = parameter.上升时间.ToString();
                        viewModel.LcKeepTime = parameter.测试时间.ToString();
                        viewModel.LcMin = parameter.下限值?.ToString();
                        viewModel.LcMax = parameter.上限值?.ToString();
                        break;
                    case AnGuiYiEnum.TestMode.停止测试:
                        viewModel.MenuIndex = 4;
                        break;
                    default:
                        break;
                }
            }
        }
        //仪器对象
        public Interfaces.IJueYuanNaiYa Device;

        private void IrClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? vol = null;double? up = null;double? keep = null;double? down = null;
            if (!DataConvert.GetDouble(viewModel.IrVol, ref vol) || vol == null)
            {
                View.Controls.Dialogs.TipView.Show("电压值输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.IrUpTime, ref up) || up == null)
            {
                View.Controls.Dialogs.TipView.Show("上升时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.IrKeepTime, ref keep) || keep == null)
            {
                View.Controls.Dialogs.TipView.Show("测试时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.IrDownTime, ref down) || down == null)
            {
                View.Controls.Dialogs.TipView.Show("下降时间输入有误!");
                return;
            }
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.IrMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.IrMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            JueYuanNaiYa parameter = new JueYuanNaiYa()
            {
                mode = AnGuiYiEnum.TestMode.绝缘电阻,
                电压值 = (double)vol,
                上升时间 = (double)up,
                测试时间 = (double)keep,
                下降时间 = (double)down,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void DcwClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? vol = null; double? up = null; double? keep = null; double? down = null; double? arc = null;
            if (!DataConvert.GetDouble(viewModel.DcwVol, ref vol) || vol == null)
            {
                View.Controls.Dialogs.TipView.Show("电压值输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DcwUpTime, ref up) || up == null)
            {
                View.Controls.Dialogs.TipView.Show("上升时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DcwKeepTime, ref keep) || keep == null)
            {
                View.Controls.Dialogs.TipView.Show("测试时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DcwDownTime, ref down) || down == null)
            {
                View.Controls.Dialogs.TipView.Show("下降时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DcwArc, ref arc) || arc == null)
            {
                View.Controls.Dialogs.TipView.Show("电弧输入有误!");
                return;
            }
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.DcwMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.DcwMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            JueYuanNaiYa parameter = new JueYuanNaiYa()
            {
                mode = AnGuiYiEnum.TestMode.直流耐压,
                电压值 = (double)vol,
                上升时间 = (double)up,
                测试时间 = (double)keep,
                下降时间 = (double)down,
                电弧值 = arc ?? 0,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void AcwClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? vol = null; double? up = null; double? keep = null; double? down = null; double? arc = null;
            if (!DataConvert.GetDouble(viewModel.AcwVol, ref vol) || vol == null)
            {
                View.Controls.Dialogs.TipView.Show("电压值输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.AcwUpTime, ref up) || up == null)
            {
                View.Controls.Dialogs.TipView.Show("上升时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.AcwKeepTime, ref keep) || keep == null)
            {
                View.Controls.Dialogs.TipView.Show("测试时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.AcwDownTime, ref down) || down == null)
            {
                View.Controls.Dialogs.TipView.Show("下降时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.AcwArc, ref arc) || arc == null)
            {
                View.Controls.Dialogs.TipView.Show("电弧输入有误!");
                return;
            }
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.AcwMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.AcwMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            JueYuanNaiYa parameter = new JueYuanNaiYa()
            {
                mode = AnGuiYiEnum.TestMode.交流耐压,
                电压值 = (double)vol,
                上升时间 = (double)up,
                测试时间 = (double)keep,
                下降时间 = (double)down,
                电弧值 = arc??0,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void StopClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            JueYuanNaiYa parameter = new JueYuanNaiYa()
            {
                mode = AnGuiYiEnum.TestMode.停止测试,
            };
            //根据对应串口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void LcClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? vol = null; double? up = null; double? keep = null; double? cur = null;
            if (!DataConvert.GetDouble(viewModel.LcVol, ref vol) || vol == null)
            {
                View.Controls.Dialogs.TipView.Show("电压值输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.LcCur, ref cur) || cur == null)
            {
                View.Controls.Dialogs.TipView.Show("电流值输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.LcUpTime, ref up) || up == null)
            {
                View.Controls.Dialogs.TipView.Show("上升时间输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.LcKeepTime, ref keep) || keep == null)
            {
                View.Controls.Dialogs.TipView.Show("测试时间输入有误!");
                return;
            }

            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.LcMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.LcMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            JueYuanNaiYa parameter = new JueYuanNaiYa()
            {
                mode = AnGuiYiEnum.TestMode.漏电流,
                电压值 = (double)vol,
                电流值 = (double)cur,
                上升时间 = (double)up,
                测试时间 = (double)keep,
                下限值 = min,
                上限值 = max
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }
    }
}
